<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/4/23
 * Time: 11:44
 */

namespace app\common\validate;


use think\Validate;

class Counselor extends Validate
{

    protected $field = [
        'tag_type' => '标签类型,【1占星/提问 2评论 3举报】',
        'type' => '筛选类型,【0=>全部,1=>占星师,2=>提问师】',
        'price' => '筛选价格',
        'level' => '筛选等级',
        'grade' => '筛选分数',
//        'search' => 'require',
        'name' => '用户认证真实姓名',
        'telphone' => '用户认证手机号',
        'id_number' => '用户认证身份证号',
        'left_pic' => '身份证正面照',
        'right_pic' => '身份证反面照',
        'intro' => '用户认证介绍',
        'nickname' => '咨询师花名',
        'pid' => '测评类型 【1=>占星达人,2=>提问师测评,3=>占星师测评】',
        'content' => '测评提交内容',
        'status' => '接单状态【0=>不接单,1=>接单】',
        'cid' => '咨询师id',
        'alipay_account' => '支付宝账号'
    ];

    protected $rule = [
        'type' => 'in:0,1,2',
        'price' => 'in:0,1,2,3,4',
        'level' => 'in:0,1,2,3,4',
        'grade' => 'in:0,1,2,3,4',
//        'search' => 'require',
        'name' => 'require|chsAlphaNum|length:1,25|filterWords',
        'telphone' => 'require|number',
        'id_number' => 'require|alphaNum',
        'left_pic' => 'require',
        'right_pic' => 'require',
        'intro' => 'require|length:0,100|filterWords',
        'nickname' => 'require|chsAlphaNum|length:1,10|filterWords',
        'pid' => 'require|in:1,2,3',
        'content' => 'require|filterWords',
        'status' => 'require|in:0,1',
        'cid' => 'require|number',
        'alipay_account' => 'require'
    ];

    protected $message = [
        'type.in' => '未知类型',
        'price.in' => '价格范围超出',
        'level.in' => '级别超出',
        'grade.in' => '星级未知',
        'name.require' => '姓名必填',
        'name.chsAlphaNum' => '姓名只能是汉字、字母和数字',
        'name.length' => '姓名长度超出',
        'name.filterWords' => '姓名包含敏感词',
        'telphone.require' => '手机号码必填',
        'telphone.number' => '手机号码违规',
        'id_number.require' => '身份证号码必填',
        'id_number.alphaNum' => '身份证号码违规',
        'left_pic.require' => '身份证正面照必传',
        'right_pic.require' => '身份证反面照必传',
        'intro.require' => '自我介绍必填',
        'intro.length' => '内容长度超出范围',
        'intro.filterWords' => '内容包含敏感词',
        'nickname.require' => '姓名必填',
        'nickname.chsAlphaNum' => '姓名只能是汉字、字母和数字',
        'nickname.length' => '姓名长度超出',
        'nickname.filterWords' => '姓名包含敏感词',
        'pid.require' => '测评类必填',
        'pid.in' => '测评类型未知',
        'content.require' => '测评结果信息未知',
        'content.filterWords' => '内容包含敏感词',
        'time.require' => '测评时间未知',
        'status.require' => '接单状态未知',
        'status.in' => '接单状态未知',
        'cid.require' => '未知的咨询师',
        'alipay_account.require' => '支付宝账号必填',
    ];

    protected $scene = [
        'index' => [
            'type',
            'price',
            'level',
            'grade',
        ],
        'auth' => [
            'name',
            'telphone',
            'id_number',
            'left_pic',
            'right_pic',
            'intro',
            'nickname'
        ],
        'ExamineContent' => [
            'pid'
        ],
        'SubmitExamine' => [
            'pid',
            'content',
        ],
        'IsTakeOrder' => [
            'status'
        ],
        'CounselorHome' => [
            'cid'
        ],
        'getTag' => [
            'tag_type'
        ],
        'bindAli' => [
            'name',
            'alipay_account'
        ],
        'TimeRemaining' => [
            'pid'
        ]
    ];


    /**
     * 过滤敏感词
     */
    public function filterWords($value)
    {
        $words = config('filter_words.words');
        $status = true;
        foreach ($words as $w) if (strpos($value, $w) !== false) $status = false;
        if (in_array($value, $words)) $status = false;
        return $status;
    }


    /**
     * 验证手机格式是否正确
     * @param $value
     * @return bool
     */
    public function checkTel($value)
    {
        if (preg_match("/^1[3456789]{1}\d{9}$/", $value)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 验证支付宝账号格式是否正确
     * @param $value
     * @return bool
     */
    public function checkAli($value)
    {
        if (preg_match("/^\w+([-_.]\w+)*@\w+([-_.]\w+)*(\.\w+){0,3}$/i", $value)) {
            return true;

        } elseif (preg_match("/^1[3456789]{1}\d{9}$/", $value)) {

            return true;

        } else {

            return false;

        }
    }

}